home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 97 / CD-ROM 97 / CD-ROM 97.iso / internet / ghostzilla / ghsetup.exe / chrome / comm.jar / content / editor / editorUtilities.js < prev    next >
Encoding:
Text File  |  2002-04-09  |  21.1 KB  |  854 lines

  1. /*
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  *
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  *
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Pete Collins
  22.  *   Brian King
  23.  *   Daniel Glazman <glazman@netscape.com>
  24.  */
  25.  
  26. /**** NAMESPACES ****/
  27. const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
  28.  
  29. // Each editor window must include this file
  30. // Variables  shared by all dialogs:
  31. var editorShell;
  32.  
  33. // Object to attach commonly-used widgets (all dialogs should use this)
  34. var gDialog = {};
  35.  
  36. // Bummer! Can't get at enums from nsIDocumentEncoder.h
  37. // http://lxr.mozilla.org/seamonkey/source/content/base/public/nsIDocumentEncoder.h#111
  38. var gStringBundle;
  39. var gIOService;
  40. var gPrefsService;
  41. var gPrefsBranch;
  42. var gFilePickerDirectory;
  43.  
  44. var gOS = "";
  45. const gWin = "Win";
  46. const gUNIX = "UNIX";
  47. const gMac = "Mac";
  48.  
  49. /************* Message dialogs ***************/
  50.  
  51. function AlertWithTitle(title, message, parentWindow)
  52. {
  53.   if (!parentWindow)
  54.     parentWindow = window;
  55.  
  56.   var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
  57.   promptService = promptService.QueryInterface(Components.interfaces.nsIPromptService);
  58.  
  59.   if (promptService)
  60.   {
  61.     if (!title)
  62.       title = GetString("Alert");
  63.  
  64.     // "window" is the calling dialog window
  65.     promptService.alert(parentWindow, title, message);
  66.   }
  67. }
  68.  
  69. // Optional: Caller may supply text to substitue for "Ok" and/or "Cancel"
  70. function ConfirmWithTitle(title, message, okButtonText, cancelButtonText)
  71. {
  72.   var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService();
  73.   promptService = promptService.QueryInterface(Components.interfaces.nsIPromptService);
  74.  
  75.   if (promptService)
  76.   {
  77.     var okFlag = okButtonText ? promptService.BUTTON_TITLE_IS_STRING : promptService.BUTTON_TITLE_OK;
  78.     var cancelFlag = cancelButtonText ? promptService.BUTTON_TITLE_IS_STRING : promptService.BUTTON_TITLE_CANCEL;
  79.  
  80.     return promptService.confirmEx(window, title, message,
  81.                             (okFlag * promptService.BUTTON_POS_0) +
  82.                             (cancelFlag * promptService.BUTTON_POS_1),
  83.                             okButtonText, cancelButtonText, null, null, {value:0}) == 0;
  84.   }
  85.   return false;
  86. }
  87.  
  88. /************* String Utilities ***************/
  89.  
  90. function GetString(name)
  91. {
  92.   if (!gStringBundle)
  93.   {
  94.     try {
  95.       var strBundleService =
  96.           Components.classes["@mozilla.org/intl/stringbundle;1"].getService(); 
  97.       strBundleService = 
  98.           strBundleService.QueryInterface(Components.interfaces.nsIStringBundleService);
  99.  
  100.       gStringBundle = strBundleService.createBundle("chrome://editor/locale/editor.properties"); 
  101.  
  102.     } catch (ex) {}
  103.   }
  104.   if (gStringBundle)
  105.   {
  106.     try {
  107.       return gStringBundle.GetStringFromName(name);
  108.     } catch (e) {}
  109.   }
  110.   return null;
  111. }
  112.  
  113. function TrimStringLeft(string)
  114. {
  115.   if(!string) return "";
  116.   return string.replace(/^\s+/, "");
  117. }
  118.  
  119. function TrimStringRight(string)
  120. {
  121.   if (!string) return "";
  122.   return string.replace(/\s+$/, '');
  123. }
  124.  
  125. // Remove whitespace from both ends of a string
  126. function TrimString(string)
  127. {
  128.   if (!string) return "";
  129.   return string.replace(/(^\s+)|(\s+$)/g, '')
  130. }
  131.  
  132. function IsWhitespace(string)
  133. {
  134.   return /^\s/.test(string);
  135. }
  136.  
  137. function TruncateStringAtWordEnd(string, maxLength, addEllipses)
  138. {
  139.   // Return empty if string is null, undefined, or the empty string
  140.   if (!string)
  141.     return "";
  142.  
  143.   // We assume they probably don't want whitespace at the beginning
  144.   string = string.replace(/^\s+/, '');
  145.   if (string.length <= maxLength)
  146.     return string;
  147.  
  148.   // We need to truncate the string to maxLength or fewer chars
  149.   if (addEllipses)
  150.     maxLength -= 3;
  151.   string = string.replace(RegExp("(.{0," + maxLength + "})\\s.*"), "$1")
  152.  
  153.   if (string.length > maxLength)
  154.     string = string.slice(0, maxLength);
  155.  
  156.   if (addEllipses)
  157.     string += "...";
  158.   return string;
  159. }
  160.  
  161. // Replace all whitespace characters with supplied character
  162. // E.g.: Use charReplace = " ", to "unwrap" the string by removing line-end chars
  163. //       Use charReplace = "_" when you don't want spaces (like in a URL)
  164. function ReplaceWhitespace(string, charReplace)
  165. {
  166.   return string.replace(/(^\s+)|(\s+$)/g,'').replace(/\s+/g,charReplace)
  167. }
  168.  
  169. // Replace whitespace with "_" and allow only HTML CDATA
  170. //   characters: "a"-"z","A"-"Z","0"-"9", "_", ":", "-", ".",
  171. //   and characters above ASCII 127
  172. function ConvertToCDATAString(string)
  173. {
  174.   return string.replace(/\s+/g,"_").replace(/[^a-zA-Z0-9_\.\-\:\u0080-\uFFFF]+/g,'');
  175. }
  176.  
  177. function GetSelectionAsText()
  178. {
  179.   return editorShell.GetContentsAs("text/plain", 1); // OutputSelectionOnly
  180. }
  181.  
  182.  
  183. /************* Element enbabling/disabling ***************/
  184.  
  185. // this function takes an elementID and a flag
  186. // if the element can be found by ID, then it is either enabled (by removing "disabled" attr)
  187. // or disabled (setAttribute) as specified in the "doEnable" parameter
  188. function SetElementEnabledById(elementID, doEnable)
  189. {
  190.   SetElementEnabled(document.getElementById(elementID), doEnable);
  191. }
  192.  
  193. function SetElementEnabled(element, doEnable)
  194. {
  195.   if ( element )
  196.   {
  197.     if ( doEnable )
  198.       element.removeAttribute("disabled");
  199.     else
  200.       element.setAttribute("disabled", "true");
  201.   }
  202.   else
  203.   {
  204.     dump("Element  not found in SetElementEnabled\n");
  205.   }
  206. }
  207.  
  208. function SetElementHidden(element, hide)
  209. {
  210.   if (element)
  211.   {
  212.     if (hide)
  213.       element.setAttribute("hidden", "true");
  214.     else
  215.       element.removeAttribute("hidden");
  216.   }
  217. }
  218.  
  219. function DisableItem(id, disable)
  220. {
  221.   var item = document.getElementById(id);
  222.   if (item)
  223.   {
  224.     if (disable != (item.getAttribute("disabled") == "true"))
  225.       item.setAttribute("disabled", disable ? "true" : "");
  226.   }
  227. }
  228.  
  229.  
  230. /************* Services / Prefs ***************/
  231.  
  232. function GetIOService()
  233. {
  234.   if (gIOService)
  235.     return gIOService;
  236.  
  237.   gIOService = Components.classes["@mozilla.org/network/io-service;1"]
  238.                .getService(Components.interfaces.nsIIOService);
  239.  
  240.   return gIOService;
  241. }
  242.  
  243. function GetPrefsService()
  244. {
  245.   if (gPrefsService)
  246.     return gPrefsService;
  247.  
  248.   try {
  249.     gPrefsService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
  250.   }
  251.   catch(ex) {
  252.     dump("failed to get prefs service!\n");
  253.   }
  254.  
  255.   return gPrefsService;
  256. }
  257.  
  258. function GetPrefs()
  259. {
  260.   if (gPrefsBranch)
  261.     return gPrefsBranch;
  262.  
  263.   try {
  264.     var prefService = GetPrefsService();
  265.     if (prefService)
  266.       gPrefsBranch = prefService.getBranch(null);
  267.  
  268.     if (gPrefsBranch)
  269.       return gPrefsBranch;
  270.     else
  271.       dump("failed to get root prefs!\n");
  272.   }
  273.   catch(ex) {
  274.     dump("failed to get root prefs!\n");
  275.   }
  276.   return null;
  277. }
  278.  
  279. function SetUnicharPref(aPrefName, aPrefValue)
  280. {
  281.   var prefs = GetPrefs();
  282.   if (prefs)
  283.   {
  284.     try {
  285.       var str = Components.classes["@mozilla.org/supports-wstring;1"]
  286.                           .createInstance(Components.interfaces.nsISupportsWString);
  287.       str.data = aPrefValue;
  288.       prefs.setComplexValue(aPrefName, Components.interfaces.nsISupportsWString, str);
  289.     }
  290.     catch(e) {}
  291.   }
  292. }
  293.  
  294. function GetUnicharPref(aPrefName, aDefVal)
  295. {
  296.   var prefs = GetPrefs();
  297.   if (prefs)
  298.   {
  299.     try {
  300.       return prefs.getComplexValue(aPrefName, Components.interfaces.nsISupportsWString).data;
  301.     }
  302.     catch(e) {}
  303.   }
  304.   return "";
  305. }
  306.  
  307. // Set initial directory for a filepicker from URLs saved in prefs
  308. function SetFilePickerDirectory(filePicker, fileType)
  309. {
  310.   if (filePicker)
  311.   {
  312.     try {
  313.       var prefBranch = GetPrefs();
  314.       if (prefBranch)
  315.       {
  316.         // Save current directory so we can reset it in SaveFilePickerDirectory
  317.         gFilePickerDirectory = filePicker.displayDirectory;
  318.  
  319.         var location = prefBranch.getComplexValue("editor.lastFileLocation."+fileType, Components.interfaces.nsILocalFile);
  320.         if (location)
  321.           filePicker.displayDirectory = location;
  322.       }
  323.     }
  324.     catch(e) {}
  325.   }
  326. }
  327.  
  328. // Save the directory of the selected file to prefs
  329. function SaveFilePickerDirectory(filePicker, fileType)
  330. {
  331.   if (filePicker && filePicker.file)
  332.   {
  333.     try {
  334.       var prefBranch = GetPrefs();
  335.  
  336.       var fileDir;
  337.       if (filePicker.file.parent)
  338.         fileDir = filePicker.file.parent.QueryInterface(Components.interfaces.nsILocalFile);
  339.  
  340.       if (prefBranch)
  341.        prefBranch.setComplexValue("editor.lastFileLocation."+fileType, Components.interfaces.nsILocalFile, fileDir);
  342.     
  343.       var prefsService = GetPrefsService();
  344.         prefsService.savePrefFile(null);
  345.     } catch (e) {}
  346.   }
  347.  
  348.   // Restore the directory used before SetFilePickerDirectory was called;
  349.   // This reduces interference with Browser and other module directory defaults
  350.   if (gFilePickerDirectory)
  351.     filePicker.displayDirectory = gFilePickerDirectory;
  352.  
  353.   gFilePickerDirectory = null;
  354. }
  355.  
  356. function GetDefaultBrowserColors()
  357. {
  358.   var prefs = GetPrefs();
  359.   var colors = new Object();
  360.   var useSysColors = false;
  361.   colors.TextColor = 0;
  362.   colors.BackgroundColor = 0;
  363.   try { useSysColors = prefs.getBoolPref("browser.display.use_system_colors"); } catch (e) {}
  364.  
  365.   if (!useSysColors)
  366.   {
  367.     try { colors.TextColor = prefs.getCharPref("browser.display.foreground_color"); } catch (e) {}
  368.  
  369.     try { colors.BackgroundColor = prefs.getCharPref("browser.display.background_color"); } catch (e) {}
  370.   }
  371.   // Use OS colors for text and background if explicitly asked or pref is not set
  372.   if (!colors.TextColor)
  373.     colors.TextColor = "windowtext";
  374.  
  375.   if (!colors.BackgroundColor)
  376.     colors.BackgroundColor = "window";
  377.  
  378.   colors.LinkColor = prefs.getCharPref("browser.anchor_color");
  379.   colors.VisitedLinkColor = prefs.getCharPref("browser.visited_color");
  380.  
  381.   return colors;
  382. }
  383.  
  384. /************* URL handling ***************/
  385.  
  386. function TextIsURI(selectedText)
  387. {
  388.   if (selectedText)
  389.   {
  390.     var text = selectedText.toLowerCase();
  391.     return text.match(/^http:\/\/|^https:\/\/|^file:\/\/|^ftp:\/\/|\
  392.                       ^about:|^mailto:|^news:|^snews:|^telnet:|\
  393.                       ^ldap:|^ldaps:|^gopher:|^finger:|^javascript:/);
  394.   }
  395.   return false;
  396. }
  397.  
  398. function IsUrlAboutBlank(urlString)
  399. {
  400.   return (urlString == "about:blank");
  401. }
  402.  
  403. function MakeRelativeUrl(url)
  404. {
  405.   var inputUrl = TrimString(url);
  406.   if (!inputUrl)
  407.     return inputUrl;
  408.  
  409.   // Get the filespec relative to current document's location
  410.   // NOTE: Can't do this if file isn't saved yet!
  411.   var docUrl = GetDocumentBaseUrl();
  412.   var docScheme = GetScheme(docUrl);
  413.  
  414.   // Can't relativize if no doc scheme (page hasn't been saved)
  415.   if (!docScheme)
  416.     return inputUrl;
  417.  
  418.   var urlScheme = GetScheme(inputUrl);
  419.  
  420.   // Do nothing if not the same scheme or url is already relativized
  421.   if (docScheme != urlScheme)
  422.     return inputUrl;
  423.  
  424.   var IOService = GetIOService();
  425.   if (!IOService)
  426.     return inputUrl;
  427.  
  428.   // Host must be the same
  429.   var docHost = GetHost(docUrl);
  430.   var urlHost = GetHost(inputUrl);
  431.   if (docHost != urlHost)
  432.     return inputUrl;
  433.  
  434.  
  435.   // Get just the file path part of the urls
  436.   var docPath = IOService.extractUrlPart(docUrl, IOService.url_Path); 
  437.   var urlPath = IOService.extractUrlPart(inputUrl, IOService.url_Path);
  438.  
  439.   // We only return "urlPath", so we can convert
  440.   //  the entire docPath for case-insensitive comparisons
  441.   var os = GetOS();
  442.   var doCaseInsensitive = (docScheme == "file" && os == gWin);
  443.   if (doCaseInsensitive)
  444.     docPath = docPath.toLowerCase();
  445.  
  446.   // Get document filename before we start chopping up the docPath
  447.   var docFilename = GetFilename(docPath);
  448.  
  449.   // Both url and doc paths now begin with "/"
  450.   // Look for shared dirs starting after that
  451.   urlPath = urlPath.slice(1);
  452.   docPath = docPath.slice(1);
  453.  
  454.   var firstDirTest = true;
  455.   var nextDocSlash = 0;
  456.   var done = false;
  457.  
  458.   // Remove all matching subdirs common to both doc and input urls
  459.   do {
  460.     nextDocSlash = docPath.indexOf("\/");
  461.     var nextUrlSlash = urlPath.indexOf("\/");
  462.  
  463.     if (nextUrlSlash == -1)
  464.     {
  465.       // We're done matching and all dirs in url
  466.       // what's left is the filename
  467.       done = true;
  468.  
  469.       // Remove filename for named anchors in the same file
  470.       if (nextDocSlash == -1 && docFilename)
  471.       { 
  472.         var anchorIndex = urlPath.indexOf("#");
  473.         if (anchorIndex > 0)
  474.         {
  475.           var urlFilename = doCaseInsensitive ? urlPath.toLowerCase() : urlPath;
  476.         
  477.           if (urlFilename.indexOf(docFilename) == 0)
  478.             urlPath = urlPath.slice(anchorIndex);
  479.         }
  480.       }
  481.     }
  482.     else if (nextDocSlash >= 0)
  483.     {
  484.       // Test for matching subdir
  485.       var docDir = docPath.slice(0, nextDocSlash);
  486.       var urlDir = urlPath.slice(0, nextUrlSlash);
  487.       if (doCaseInsensitive)
  488.         urlDir = urlDir.toLowerCase();
  489.  
  490.       if (urlDir == docDir)
  491.       {
  492.  
  493.         // Remove matching dir+"/" from each path
  494.         //  and continue to next dir
  495.         docPath = docPath.slice(nextDocSlash+1);
  496.         urlPath = urlPath.slice(nextUrlSlash+1);
  497.       }
  498.       else
  499.       {
  500.         // No match, we're done
  501.         done = true;
  502.  
  503.         // Be sure we are on the same local drive or volume 
  504.         //   (the first "dir" in the path) because we can't 
  505.         //   relativize to different drives/volumes.
  506.         // UNIX doesn't have volumes, so we must not do this else
  507.         //  the first directory will be misinterpreted as a volume name
  508.         if (firstDirTest && docScheme == "file" && os != gUNIX)
  509.           return inputUrl;
  510.       }
  511.     }
  512.     else  // No more doc dirs left, we're done
  513.       done = true;
  514.  
  515.     firstDirTest = false;
  516.   }
  517.   while (!done);
  518.  
  519.   // Add "../" for each dir left in docPath
  520.   while (nextDocSlash > 0)
  521.   {
  522.     urlPath = "../" + urlPath;
  523.     nextDocSlash = docPath.indexOf("\/", nextDocSlash+1);
  524.   }
  525.   return urlPath;
  526. }
  527.  
  528. function MakeAbsoluteUrl(url)
  529. {
  530.   var resultUrl = TrimString(url);
  531.   if (!resultUrl)
  532.     return resultUrl;
  533.  
  534.   // Check if URL is already absolute, i.e., it has a scheme
  535.   var urlScheme = GetScheme(resultUrl);
  536.  
  537.   if (urlScheme)
  538.     return resultUrl;
  539.  
  540.   var docUrl = GetDocumentBaseUrl();
  541.   var docScheme = GetScheme(docUrl);
  542.  
  543.   // Can't relativize if no doc scheme (page hasn't been saved)
  544.   if (!docScheme)
  545.     return resultUrl;
  546.  
  547.   var  IOService = GetIOService();
  548.   if (!IOService)
  549.     return resultUrl;
  550.   
  551.   // Make a URI object to use its "resolve" method
  552.   var absoluteUrl = resultUrl;
  553.   var docUri = IOService.newURI(docUrl, null, null);
  554.  
  555.   try {
  556.     absoluteUrl = docUri.resolve(resultUrl);
  557.     // This is deprecated and buggy! 
  558.     // If used, we must make it a path for the parent directory (remove filename)
  559.     //absoluteUrl = IOService.resolveRelativePath(resultUrl, docUrl);
  560.   } catch (e) {}
  561.  
  562.   return absoluteUrl;
  563. }
  564.  
  565. // Get the HREF of the page's <base> tag or the document location
  566. // returns empty string if no base href and document hasn't been saved yet
  567. function GetDocumentBaseUrl()
  568. {
  569.   if (window.editorShell)
  570.   {
  571.     var docUrl;
  572.  
  573.     // if document supplies a <base> tag, use that URL instead 
  574.     var baseList = editorShell.editorDocument.getElementsByTagName("base");
  575.     if (baseList)
  576.     {
  577.       var base = baseList.item(0);
  578.       if (base)
  579.         docUrl = base.getAttribute("href");
  580.     }
  581.     if (!docUrl)
  582.       docUrl = GetDocumentUrl();
  583.  
  584.     if (!IsUrlAboutBlank(docUrl))
  585.       return docUrl;
  586.   }
  587.   return "";
  588. }
  589.  
  590. function GetDocumentUrl()
  591. {
  592.   if (editorShell && editorShell.editorDocument)
  593.   {
  594.     try {
  595.       var aDOMHTMLDoc = editorShell.editorDocument.QueryInterface(Components.interfaces.nsIDOMHTMLDocument);
  596.       return aDOMHTMLDoc.URL;
  597.     }
  598.     catch (e) {}
  599.   }
  600.   return "";
  601. }
  602.  
  603. // Extract the scheme (e.g., 'file', 'http') from a URL string
  604. function GetScheme(url)
  605. {
  606.   var resultUrl = TrimString(url);
  607.   // Unsaved document URL has no acceptable scheme yet
  608.   if (!resultUrl || IsUrlAboutBlank(resultUrl))
  609.     return "";
  610.  
  611.   if (/^internal-gopher-/.test(resultUrl))
  612.     return "internal-gopher-";
  613.  
  614.   var IOService = GetIOService();
  615.   if (!IOService)
  616.     return "";
  617.  
  618.   var scheme = "";
  619.   try {
  620.     // This fails if there's no scheme
  621.     scheme = IOService.extractScheme(resultUrl, {schemeStartPos:0}, {schemeEndPos:0});
  622.   } catch (e) {}
  623.  
  624.   return scheme ? scheme.toLowerCase() : "";
  625. }
  626.  
  627. function GetHost(url)
  628. {
  629.   if (!url)
  630.     return "";
  631.  
  632.   var IOService = GetIOService();
  633.   if (!IOService)
  634.     return "";
  635.  
  636.   var host = "";
  637.   try {
  638.     host = IOService.extractUrlPart(url, IOService.url_Host); 
  639.    } catch (e) {}
  640.  
  641.   return host;
  642. }
  643.  
  644. function GetUsername(url)
  645. {
  646.   if (!url)
  647.     return "";
  648.  
  649.   var IOService = GetIOService();
  650.   if (!IOService)
  651.     return "";
  652.  
  653.   var username = "";
  654.   try {
  655.     username = IOService.extractUrlPart(url, IOService.url_Username);
  656.   } catch (e) {}
  657.  
  658.   return username;
  659. }
  660.  
  661. function GetFilename(url)
  662. {
  663.   if (!url || IsUrlAboutBlank(url))
  664.     return "";
  665.  
  666.   var IOService = GetIOService();
  667.   if (!IOService)
  668.     return "";
  669.  
  670.   var filename;
  671.  
  672.   try {
  673.     filename = IOService.extractUrlPart(url, IOService.url_FileBaseName);
  674.     if (filename)
  675.     {
  676.       var ext = IOService.extractUrlPart(url, IOService.url_FileExtension);
  677.       if (ext)
  678.         filename += "."+ext;
  679.     }
  680.   } catch (e) {}
  681.  
  682.   return filename ? filename : "";
  683. }
  684.  
  685. // Return the url without username and password
  686. // Optional output objects return extracted username and password strings
  687. // This uses just string routines via nsIIOServices
  688. function StripUsernamePassword(url, usernameObj, passwordObj)
  689. {
  690.   url = TrimString(url);
  691.   if (!url || IsUrlAboutBlank(url))
  692.     return url;
  693.  
  694.   if (usernameObj)
  695.     usernameObj.value = "";
  696.   if (passwordObj)
  697.     passwordObj.value = "";
  698.  
  699.   // "@" must exist else we will never detect username or password
  700.   var atIndex = url.indexOf("@");
  701.   if (atIndex > 0)
  702.   {
  703.     try {
  704.       var IOService = GetIOService();
  705.       if (!IOService)
  706.         return url;
  707.  
  708.       var username = IOService.extractUrlPart(url, IOService.url_Username);
  709.       var password = IOService.extractUrlPart(url, IOService.url_Password);
  710.  
  711.       if (usernameObj && username)
  712.         usernameObj.value = username;
  713.       if (passwordObj && password)
  714.         passwordObj.value = password;
  715.       if (username)
  716.       {
  717.         var usernameStart = url.indexOf(username);
  718.         if (usernameStart != -1)
  719.           return url.slice(0, usernameStart) + url.slice(atIndex+1);
  720.       }
  721.     } catch (e) {}
  722.   }
  723.   return url;
  724. }
  725.  
  726. function StripPassword(url, passwordObj)
  727. {
  728.   url = TrimString(url);
  729.   if (!url || IsUrlAboutBlank(url))
  730.     return url;
  731.  
  732.   if (passwordObj)
  733.     passwordObj.value = "";
  734.  
  735.   // "@" must exist else we will never detect password
  736.   var atIndex = url.indexOf("@");
  737.   if (atIndex > 0)
  738.   {
  739.     try {
  740.       var IOService = GetIOService();
  741.       if (!IOService)
  742.         return url;
  743.  
  744.       var password = IOService.extractUrlPart(url, IOService.url_Password);
  745.  
  746.       if (passwordObj && password)
  747.         passwordObj.value = password;
  748.       if (password)
  749.       {
  750.         // Find last ":" before "@"
  751.         var colon = url.lastIndexOf(":", atIndex);
  752.         if (colon != -1)
  753.         {
  754.           // Include the "@"
  755.           return url.slice(0, colon) + url.slice(atIndex);
  756.         }
  757.       }
  758.     } catch (e) {}
  759.   }
  760.   return url;
  761. }
  762.  
  763. // Version to use when you have an nsIURI object
  764. function StripUsernamePasswordFromURI(uri)
  765. {
  766.   var url = "";
  767.   if (uri)
  768.   {
  769.     try {
  770.       url = uri.spec;
  771.       var userPass = uri.userPass;
  772.       if (userPass)
  773.       {
  774.         start = url.indexOf(userPass);
  775.         url = url.slice(0, start) + url.slice(start+userPass.length+1);
  776.       }
  777.     } catch (e) {}    
  778.   }
  779.   return url;
  780. }
  781.  
  782. function InsertUsernameIntoUrl(url, username)
  783. {
  784.   if (!url || !username)
  785.     return url;
  786.  
  787.   try {
  788.     var ioService = GetIOService();
  789.     var URI = ioService.newURI(url, window.editorShell.GetDocumentCharacterSet(), null);
  790.     URI.username = username;
  791.     return URI.spec;
  792.   } catch (e) {}
  793.  
  794.   return url;
  795. }
  796.  
  797. function GetOS()
  798. {
  799.   if (gOS)
  800.     return gOS;
  801.  
  802.   var platform = navigator.platform.toLowerCase();
  803.  
  804.   if (platform.indexOf("win") != -1)
  805.     gOS = gWin;
  806.   else if (platform.indexOf("mac") != -1)
  807.     gOS = gMac;
  808.   else if (platform.indexOf("unix") != -1 || platform.indexOf("linux") != -1 || platform.indexOf("sun") != -1)
  809.     gOS = gUNIX;
  810.   else
  811.     gOS = "";
  812.   // Add other tests?
  813.  
  814.   return gOS;
  815. }
  816.  
  817. function ConvertRGBColorIntoHEXColor(color)
  818. {
  819.   if (color.search( /rgb.*/ ) != -1)
  820.   {
  821.     var res = color.match( /rgb\((\d*),(\d*),(\d*)\)/ );
  822.     var r = Number(RegExp.$1).toString(16);
  823.     if (r.length == 1) r = "0"+r;
  824.     var g = Number(RegExp.$2).toString(16);
  825.     if (g.length == 1) g = "0"+g;
  826.     var b = Number(RegExp.$3).toString(16);
  827.     if (b.length == 1) b = "0"+b;
  828.     return "#"+r+g+b;
  829.   }
  830.   else
  831.   {
  832.     return color;
  833.   }
  834. }
  835.  
  836. /************* CSS ***************/
  837.  
  838. function GetHTMLOrCSSStyleValue(element, attrName, cssPropertyName)
  839. {
  840.   var prefs = GetPrefs();
  841.   var IsCSSPrefChecked = prefs.getBoolPref("editor.use_css");
  842.   var value;
  843.   if (IsCSSPrefChecked && editorShell.editorType == "html")
  844.     value = element.style.getPropertyValue(cssPropertyName);
  845.  
  846.   if (!value)
  847.     value = element.getAttribute(attrName);
  848.  
  849.   if (!value)
  850.     return "";
  851.  
  852.   return value;
  853. }
  854.